<template>
  <!-- 分页器组件 -->
  <div class="pager">
    <div class="pager-wrap">
      <!-- 上一页 -->
      <div
        class="pager-prev"
        @click="$emit('getCurrentPage', currentPage - 1)"
        :class="{ 'pager-prev-not': currentPage == 1 }"
      >
        上一页
      </div>

      <!-- 第一页 -->
      <div
        class="item"
        v-show="startAndEnd.start > 1"
        @click="$emit('getCurrentPage', 1)"
        :class="currentPage == 1 ? 'active' : ''"
      >
        1
      </div>
      <!-- 三个点省略的位置 -->
      <div class="item-spec" v-show="startAndEnd.start > 2">...</div>

      <!-- 连续页数的位置 -->
      <div
        class="item"
        v-for="(number, index) in startAndEnd.end"
        :key="index"
        v-show="number >= startAndEnd.start"
        @click="$emit('getCurrentPage', number)"
        :class="currentPage == number ? 'active' : ''"
      >
        {{ number }}
      </div>

      <!-- 三个点省略的位置 -->
      <div class="item-spec" v-show="startAndEnd.end < totalPage - 1">...</div>
      <!-- 最后一页的位置 -->
      <div
        class="item"
        v-show="startAndEnd.end < totalPage"
        @click="$emit('getCurrentPage', totalPage)"
        :class="currentPage == totalPage ? 'active' : ''"
      >
        {{ totalPage }}
      </div>

      <!-- 下一页 -->
      <div
        class="pager-next"
        @click="$emit('getCurrentPage', currentPage + 1)"
        :class="{ 'pager-next-not': currentPage == totalPage }"
      >
        下一页
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "pager",
  props: ["currentPage", "pageSize", "pageGroup", "total"],
  data() {
    return {
      // 控制是否显示左侧三个点
      isLeftDot: false,
    };
  },
  computed: {
    // 共有多少页
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    // 起始位置和结束位置
    startAndEnd() {
      let start = 0,
        end = 0;
      let { currentPage, pageGroup, totalPage } = this;
      // 如果连续页码数大于总页数
      if (pageGroup > totalPage) {
        start = 1;
        end = totalPage;
      } else {
        start = currentPage - Math.floor(pageGroup / 2);
        if (start < 1) {
          start = 1;
          end = pageGroup;
        }
        end = currentPage + Math.floor(pageGroup / 2);
        if (end > totalPage) {
          end = totalPage;
          start = totalPage - pageGroup + 1;
        }
      }
      return { start, end };
    },
  },
};
</script>

<style scoped lang="scss">
.pager {
  width: 100%;
  display: grid;
  justify-items: center;
  .pager-wrap {
    display: flex;
    font-size: 12px;
    line-height: 22px;
    margin: 25px 0;
    // 高亮
    .active {
      background-color: #c20c0c;
      color: #fff;
    }
    // 上一页禁用
    .pager-prev-not {
      background-position: 0 -621px !important;
      cursor: default !important;
      color: #c0c0c0 !important;
    }
    // 下一页禁用
    .pager-next-not {
      background-position: -75px -621px !important;
      cursor: default !important;
      color: #c0c0c0 !important;
    }
    .pager-prev,
    .pager-next {
      width: 47px;
      height: 22px;
      border: 1px solid #ccc;
      border-radius: 2px;
      cursor: pointer;
      background: url("@/assets/button.png") no-repeat;
    }
    .pager-prev {
      background-position: 0 -561px;
      padding-left: 22px;
      &:hover {
        background-position: 0 -591px;
      }
    }
    .pager-next {
      background-position: -75px -561px;
      padding-right: 22px;
      text-align: right;
      &:hover {
        background-position: -75px -591px;
      }
    }
    .item {
      width: 22px;
      height: 22px;
      border: 1px solid #ccc;
      border-radius: 2px;
      margin: 0 2px;
      text-align: center;
      cursor: pointer;
    }
    .item-spec {
      color: #333;
    }
  }
}
</style>